﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BL;
using BO;

namespace AnimalFarm.Field_Building_manage
{
    public partial class AddNewField : Form
    {
        int farmID = 0;
        public AddNewField()
        {
            InitializeComponent();
            loadFieldType();
        }

        public AddNewField(int farmID)
        {
            InitializeComponent();
            this.farmID = farmID;
            loadFieldType();
        }

        private void btnCreate_Click(object sender, EventArgs e)
        {
            int fieldTypeID = Int32.Parse(ddlFieldType.SelectedValue.ToString());
            string fieldName = txtFieldName.Text;
            string location = txtLocation.Text;
            B_Field bf = B_Field.CreateInstance();
            Field_Info fi = new Field_Info();
            fi.FarmID = farmID;
            fi.FieldName = fieldName;
            fi.FieldTypeID = fieldTypeID;
            fi.Location = location;
            if (fi != null)
            {
                bool isEffected = bf.CreateOneField(fi);
                if (isEffected == true)
                {
                    MessageBox.Show("Has created a new Field successfully!", "Adding Field Completely", MessageBoxButtons.OK);
                    txtFieldName.Text = "";
                    txtLocation.Text = "";
                    ddlFieldType.SelectedIndex = 0;
                }
                else
                {
                    MessageBox.Show("Cannot created a new Field!", "Missing Data", MessageBoxButtons.OK);
                    txtFieldName.Focus();
                }
            }
        }

        private void loadFieldType()
        {
            B_FieldType bft = B_FieldType.CreateInstance();
            DataTable dTable = bft.GetAllFieldType();
            ddlFieldType.DataSource = dTable;
            ddlFieldType.DisplayMember = "FieldTypeName";
            ddlFieldType.ValueMember = "FieldTypeID";
        }
    }
}
